﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
using System.IO;
namespace GUI
{
    public partial class uc_TimKiemDT : DevExpress.XtraEditors.XtraUserControl
    {
        private MobileBL mobileBl = new MobileBL();
        public uc_TimKiemDT()
        {
            InitializeComponent();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string field = txtNoiDungTK.Text;
            if (radioGroup1.SelectedIndex == 0)
            {
                gridDienThoai.DataSource = mobileBl.Mobile_SelectByBrand(field);
            } 
            else if(radioGroup1.SelectedIndex == 1)
            {
                gridDienThoai.DataSource = mobileBl.Mobile_SelectByName(field);
            }
            else if (radioGroup1.SelectedIndex == 2)
            {
                gridDienThoai.DataSource = mobileBl.Mobile_SelectByID(int.Parse(field));
            }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            txtTenDT.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Name").ToString();
            txtBaoHanh.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Brand").ToString();
            txtGiaBan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Prize").ToString();
            txtMauSac.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Color").ToString();
            txtMoTa.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Description").ToString();
            txtSoLuongTon.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Count").ToString();
            txtNhanHieu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Brand").ToString();
            txtTinhTrang.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "State").ToString();         
            int imageid = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ImageID").ToString());                    
            ImageBL imgbl = new ImageBL();
            ImageDTO imgdto = new ImageDTO();
            imgdto = imgbl.Image_SelectByID(imageid);
            if (imgdto.Image == null)
                picHinhanh.Image = null;
            else
            {
                MemoryStream st = new MemoryStream();
                st.Write(imgdto.Image, 0, imgdto.Image.Length);
                Image i = Image.FromStream(st);
                picHinhanh.Image = resizeImage(i, picHinhanh.Width, picHinhanh.Height);
            }

        }
        public Image resizeImage(Image img, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);

            g.DrawImage(img, 0, 0, width, height);
            g.Dispose();

            return (Image)b;
        }





        private void uc_TimKiemDT_Load(object sender, EventArgs e)
        {
            mobileTableAdapter.Fill(sellingmobilemanagementDataSet.mobile);
        }

        private void txtNoiDungTK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                btnTimKiem_Click(null, null);
            }
        }
    }
}
